package org.kzwl.service.web.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.kzwl.comm.advice.BizException;
import org.kzwl.comm.advice.Consts;
import org.kzwl.comm.pojo.ResponseData;
import org.kzwl.comm.pojo.bo.QuerySaveVolunteersSignBO;
import org.kzwl.comm.pojo.bo.VolunteersAuditBO;
import org.kzwl.comm.pojo.dto.QuerySaveVolunteersSignDTO;
import org.kzwl.comm.utils.MyUtils;
import org.kzwl.entity.app.User;
import org.kzwl.entity.web.CuVolunteers;
import org.kzwl.entity.web.CuVolunteersSign;
import org.kzwl.mapper.app.UserMapper;
import org.kzwl.mapper.web.CuVolunteersMapper;
import org.kzwl.mapper.web.CuVolunteersSignMapper;
import org.kzwl.service.web.CuVenuesService;
import org.kzwl.service.web.VolunteersSignService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;


@Service
public class CuVolunteersSignServiceImpl extends ServiceImpl<CuVolunteersSignMapper, CuVolunteersSign> implements VolunteersSignService {


    @Resource
    CuVolunteersSignMapper cuVolunteersSignMapper;

    @Autowired
    CuVolunteersMapper volunteersMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseData<IPage<QuerySaveVolunteersSignDTO>> selectPageAll(QuerySaveVolunteersSignBO querySaveVolunteersSignBO) {

        IPage<QuerySaveVolunteersSignDTO> objectPage = new Page<>(querySaveVolunteersSignBO.getCurrent(), querySaveVolunteersSignBO.getSize());
        IPage<QuerySaveVolunteersSignDTO> saveVolunteersSignDTOIPage = this.cuVolunteersSignMapper.selectPageAll(objectPage, querySaveVolunteersSignBO);
        return new ResponseData<>(saveVolunteersSignDTOIPage);
    }

    @Override
    public ResponseData<Void> volunteersAudit(VolunteersAuditBO volunteersAuditBO) {
        CuVolunteersSign volunteersSign = this.baseMapper.selectById(volunteersAuditBO.getCuVolunteersSignId());
        if (ObjectUtil.isNull(volunteersSign))
        {
            throw new BizException("查无此信息");
        }
//        if (volunteersAuditBO.getCuVolunteersUserId() != volunteersSign.getCuVolunteersUserId())
//        {
//            throw new BizException("信息混乱");
//        }

        User user = userMapper.selectById(volunteersSign.getCuUserId());
        if (user == null){
            throw new BizException("用户信息有误");
        }

        CuVolunteersSign cuVolunteersSign = new CuVolunteersSign();
        cuVolunteersSign.setCuSignExamineDate(LocalDateTime.now());
        BeanUtils.copyProperties(volunteersAuditBO,cuVolunteersSign);
        this.updateById(cuVolunteersSign);


        CuVolunteers cuVolunteers = volunteersMapper.selectById(volunteersSign.getCuVolunteersId());
        if (volunteersAuditBO.getCuSignState() == 1){
            MyUtils.sendSms(user.getCuUserPhone(),
                    "{" +
                            "\"name\":\"" + user.getCuUsername() + "\"," +
                            "\"title\":\"" + cuVolunteers.getCuVolunteersTitle() +"\"," +
                            "\"state\":\"已通过\"" +
                            "}", Consts.AliYun.SMSVOLUNTEERSSIGN);
        }else{
            MyUtils.sendSms(user.getCuUserPhone(),
                    "{" +
                            "\"name\":\"" + user.getCuUsername() + "\"," +
                            "\"title\":\"" + cuVolunteers.getCuVolunteersTitle() +"\"," +
                            "\"state\":\"未通过\"" +
                            "}", Consts.AliYun.SMSVOLUNTEERSSIGN);
        }

        return new ResponseData<>();
    }
}
